package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Api(tags = "菜品管理接口")
@Slf4j
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品接口")
    @PostMapping
    //使用缓存来优化当新增菜品时我们先删除缓存数据在进行加入缓存
    @CacheEvict(cacheNames = "Dish",allEntries = true)
    public Result<DishDTO> saveDish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        dishService.saveDish(dishDTO);
        //缓存优化当新增菜品时清除该分类下的缓存
        //redisTemplate.delete("dish_"+dishDTO.getCategoryId());
        return Result.success();
    }


    /**
     * 菜品分页查询
     * @param dishDTO
     * @return
     */

    @ApiOperation("菜品分页查询接口")
    @GetMapping("/page")
    public Result<PageResult> pages(DishPageQueryDTO dishDTO) {
        log.info("菜品分页查询：{}", dishDTO);
        PageResult pageResult = dishService.pageQuery(dishDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("批量删除接口")
    @DeleteMapping()
    @CacheEvict(cacheNames = "Dish",allEntries = true)
    public Result delete(@RequestParam List< Long> ids){
        log.info("批量删除：{}", ids);
        dishService.delete(ids);

//        //缓存优化当删除菜品时清除该分类下的缓存，获取到所有缓存的key
//        Set keys = redisTemplate.keys("dish_" + "*");//获取所有以dish_开头的key
       // redisTemplate.delete(keys);
        return Result.success();
    }

    @ApiOperation("根据ID回显查询数据接口")
    @GetMapping("/{id}")
    public Result<DishVO> getByID(@PathVariable Long id){
        log.info("根据ID回显查询数据：{}", id);
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    @ApiOperation("修改菜品接口")
    @PutMapping
    @CacheEvict(cacheNames = "Dish",allEntries = true)
    public Result upDateDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品 {}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }

    @ApiOperation("起售停售接口")
    @PostMapping("status/{status}")
    @CacheEvict(cacheNames = "Dish",allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("起售停售接口 {}", id);
        dishService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }


}
